using System;
using UnityEngine;
using UnityEngine.UIElements;
using MinesweeperPlus.Scripts.Configs;
using MinesweeperPlus.Scripts.Utils;

namespace MinesweeperPlus.Scripts.UI
{
    public sealed class MainPanel : UIElement
    {
        public new class UxmlFactory : UxmlFactory<MainPanel> { }

        #region Actions
        public Action OnPlayButtonClick;
        public Action OnShopButtonClick;
        #endregion

        #region Variables
        private Button _playButton;
        private IMGUIContainer _playIcon;
        private Button _shopButton;
        private IMGUIContainer _shopIcon;
        private VisualElement _smallButtonsPanel;
        #endregion

        #region MainPanel Methods
        public MainPanel() : base() { }
        public MainPanel(IconsConfig iconsConfig, StyleSheet styleSheet) : 
            base(iconsConfig, styleSheet) { }
        #endregion

        #region Other Methods
        protected override void UpdateColorScheme()
        {
            base.UpdateColorScheme();

            _playButton.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);            
            _playButton.SetBorderColor(ColorSchemeConfig.BorderColor);       
            _playIcon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);

            _shopButton.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);            
            _shopButton.SetBorderColor(ColorSchemeConfig.BorderColor);      
            _shopIcon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);
        }

        protected override void CreatePanel()
        {
            Panel = this;

            _playIcon = CreateIMGUIContainer(IconsConfig.PlayIcon, Constants.UI_LARGE_ICON_STYLE);
            _playButton = CreateButton(
                _playIcon, 
                () => OnPlayButtonClick?.Invoke(), 
                Constants.UI_MAIN_PLAY_BUTTON_STYLE);
            
            _smallButtonsPanel = new VisualElement();
            _smallButtonsPanel.AddToClassList(Constants.UI_MAIN_SMALL_BUTTONS_PANEL_STYLE);
            Panel.Add(_smallButtonsPanel);

            _shopIcon = CreateIMGUIContainer(IconsConfig.ShopIcon);
            _shopButton = CreateSmallButton(
                _shopIcon, 
                () => OnShopButtonClick?.Invoke());
        }

        protected override Button CreateButton(
            IMGUIContainer imguiContainer, 
            Action buttonClicked,
            params string[] classesNames)
        {
            var button = base.CreateButton(
                imguiContainer, 
                buttonClicked, 
                classesNames);
            button.AddToClassList(Constants.UI_MAIN_ELEMENT_STYLE);
            return button;
        }
        private Button CreateSmallButton(
            IMGUIContainer imguiContainer, 
            Action buttonClicked,
            params string[] classesNames)
        {
            var button = this.CreateButton(imguiContainer, buttonClicked, classesNames);
            _smallButtonsPanel.Add(button);
            imguiContainer.AddToClassList(Constants.UI_BIG_ICON_STYLE);
            return button;
        }
        #endregion
    }
}
